<?php
    session_start();
?>

<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // put your code here
        $email = $_POST["email"];
        $password = $_POST["password"];
        $displayLoginPage = true;
        
        if(strlen($email) == 0 || strlen($password) == 0){
            $errorMsg = "Invalid customer login";
        }
        
        if(strlen($email) == 0 && strlen($password) == 0){
            $errorMsg = "";
        }
        
        if(strlen($email) > 0 && strlen($password) > 0){
            $con = mysql_connect("localhost", "zhouzhao", "19831022");
            if(!$con){
                die("could not connect to DB: ".mysql_errno());
            }
            
            mysql_select_db("cloudcom", $con);
            $result = mysql_query("select * from customers where email = '$email' and password = '$password'");
            
            if(!($row = mysql_fetch_assoc($result))){
                $errorMsg = "Invalid login";
            }else{
                $displayLoginPage = false;
            }
                
        }
        
        if($displayLoginPage){
            require 'customerLogin.html';
        }else{
            mysql_query("update shoppingCarts set sessionID='".$row["customerID"]."' where sessionID='".session_id()."'");
            mysql_query("delete from shoppingCarts where sessionID=".session_id());
            
            $_SESSION["customerID"] = $row["customerID"];
            $_SESSION["firstName"] = $row["firstName"];
            $_SESSION["lastName"] = $row["lastName"];
            require 'customer.php';
        }
        
        mysql_close($con);
        ?>
    </body>
</html>
